/*
* INSANE - Interactive Structural Analysis Environment
*
* Copyright (C) 2003-2005
* Universidade Federal de Minas Gerais
* Escola de Engenharia
* Departamento de Engenharia de Estruturas
* 
* Author's email :    insane@dees.ufmg.br
* Author's website :  http://www.dees.ufmg.br/insane
* 
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
package br.ufmg.dees.insane.ui.rich.learn.learn_view.model_view.command;

import java.awt.Color;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.ArrayList;
import java.util.ListIterator;

import javax.swing.JFrame;

import br.ufmg.dees.insane.model.disc.element.Element;
import br.ufmg.dees.insane.model.disc.fem.FemModel;
import br.ufmg.dees.insane.ui.rich.learn.learn_controller.Controller;
import br.ufmg.dees.insane.ui.rich.learn.learn_view.CmdButton;
import br.ufmg.dees.insane.ui.rich.learn.learn_view.LearnInterface;
import br.ufmg.dees.insane.ui.rich.learn.learn_view.command.Command;
import br.ufmg.dees.insane.ui.rich.learn.learn_view.dialog.MatrixViewTableDialog;
import br.ufmg.dees.insane.util.IMatrix;

/**
*
* @author Moreira, Renata & Pitangueira, Roque
* @since May 2005
*/
public class SetModelReduceStiffnessCommand implements Command {

	private LearnInterface parent;
	private FemModel fmodel;
	private CmdButton[][] K;
	private CmdButton[][] kmGButtons;
	
	
	public SetModelReduceStiffnessCommand(JFrame parent) {
		this.parent = (LearnInterface)parent;
		
	}
	
	
	/** Execute method. */
	public void execute() {
		
		//Driver
		fmodel = parent.getView().getController().getModel();	
		((Controller)parent.getView().getController()).getDriver().reduceEquationsVector();
		
		//Number Format
		DecimalFormat fmt = new DecimalFormat();
	    DecimalFormatSymbols fsym = new DecimalFormatSymbols();
	    fsym.setDecimalSeparator('.');
	    fmt.setDecimalFormatSymbols(fsym);
	    fmt.applyPattern("0.000E00");
	    
		//Create the  Reduce Stiffness Matrix for each element
	    
	    ((Controller)parent.getView().getController()).getDriver().numberElementEquations();
		((Controller)parent.getView().getController()).getDriver().reduceEquationsVector();
		MatrixViewTableDialog dlg = new MatrixViewTableDialog("Element Reduce Stiffness Matrix", ((Element)fmodel.getElementsList().get(0)).globalStiffnessMatrix().getNumRow());
		ArrayList sel = this.parent.getView().getController().getSelection();
		if(sel.size()==0) parent.getView().getController().selectAll();
		ListIterator selLI = sel.listIterator();
		int c = 1;
		int size = sel.size();
		while(selLI.hasNext()){
			Element elm = (Element)selLI.next();
			IMatrix globlalK = elm.globalStiffnessMatrix();
			K = new CmdButton[globlalK.getNumRow()][globlalK.getNumCol()];
			Color cl = new Color(255*c/size, 255-(255*c/size), 140*c/size);
			for (int i=0; i<globlalK.getNumRow(); i++){
			        for (int j=0; j<globlalK.getNumCol(); j++){
			            if(elm.getReducedEquation(i) > 0 && elm.getReducedEquation(j) > 0 ){
			            	K[i][j] = new CmdButton(String.valueOf(fmt.format(globlalK.getElement(i,j))));
			            	K[i][j].setBackground(cl);
			            	K[i][j].setToolTipText("Equation ["+elm.getReducedEquation(i)+"]["+elm.getReducedEquation(j)+"] of the element "+elm.getLabel()+" Reduce Stiffness Matrix");
			    			
			            }
			            else{
			            	K[i][j]= new CmdButton("X");
			            	K[i][j].setBackground(cl);
			            	K[i][j].setToolTipText("Equation ["+elm.getReducedEquation(i)+"]["+elm.getReducedEquation(j)+"] of the element "+elm.getLabel()+" Reduce Stiffness Matrix");
			    			
			            }
			    }
			}			
			String[] colNames = new String[globlalK.getNumCol()];
			for(int k=0; k<globlalK.getNumCol(); k++)
				colNames[k] = ""+elm.getReducedEquation(k);	
			dlg.addOutputTable(elm.getLabel(),colNames,colNames,K);
			c++;
		}
		
		//Create the Reduce Stiffness Matrix for model
		
		((Controller)parent.getView().getController()).getDriver().reduceEquationsVector();
		IMatrix km = ((Controller)parent.getView().getController()).getDriver().reducedStiffnessMatrix();
		kmGButtons= new CmdButton[km.getNumRow()][km.getNumCol()];
		MatrixViewTableDialog dlg1 = new MatrixViewTableDialog("Model Reduce Stiffness Matrix", km.getNumRow());		
		
		int [] eqs = ((Controller)parent.getView().getController()).getDriver().getNodeEquations();
		int [] posEqs = new int[km.getNumCol()];
		int cont=0;
		for (int k=0; k< eqs.length; k++){
			if(eqs[k]>0){
				 posEqs[cont] = eqs[k];
				 cont ++;
			}
		}
		
		String[] colNames = new String[km.getNumCol()];
		for(int i=0; i< posEqs.length; i++){
			   colNames[i] = ""+posEqs[i];
		}
		
		for(int i=0; i<km.getNumRow(); i++)
		    for(int j=0; j<km.getNumCol(); j++){
		        kmGButtons[i][j] = new CmdButton(String.valueOf(fmt.format(km.getElement(i,j)))); 
		        kmGButtons[i][j].setBackground(Color.YELLOW);
		        kmGButtons[i][j].setCommand(new StiffnessDecompositionCommand(i, j, dlg, dlg1));
		        kmGButtons[i][j].addActionListener(parent);
		        kmGButtons[i][j].setToolTipText("Decompose the element on the Equation ["+posEqs[i]+"]["+posEqs[j]+"] of the model Reduce Stiffness Matrix");
		    }
					
		
		dlg1.addOutputTable("Model",colNames,colNames,kmGButtons);
		
		//Dialog
		
		int par = km.getNumRow();
		String model= "Model Reduce Stiffness";
		String elm=  "Element Reduce Stiffness";	
		ModelFrame f = new ModelFrame(par, dlg1, model, dlg, elm);
		f.setVisible(true);
		
		
	}
	
	
	//*****************************************************************************
	
	/** Impossible to undo - not implemented. */
	public void undo() {
	}
	
	//*****************************************************************************
	
	/** Impossible to redo - not implemented. */
	public void redo() {
	}
	
	//*****************************************************************************

	

}
